package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.ArrayAdapterInclude.CheckedViewArrayAdapter;
import com.example.myapplication.ArrayAdapterInclude.TextViewArrayAdapter;

public class ListViewActivity extends AppCompatActivity {
  String[] myData={"呵呵鼠奴你又在骗我了吧 -Adaptor","二十五万匹拳力"};
  String[] myData2={"今天天气很好呀-CheckedTextView","鼠粮两块钱便够"};
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listviewlayout);
    Toolbar toolbar = findViewById(R.id.toolbar);
    toolbar.setTitle("ListView布局");
    setSupportActionBar(toolbar);
    LinkViewByTextView();
    LinkViewByCheckedTextView();

    SetContextWindow();
  }


  @Override
  public boolean onCreateOptionsMenu(Menu menu) {

    menu.add(0,1,0,"商品菜单");
    menu.add(0,2,1,"其他");

    return true;
  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case 1:
        LoadShopView();
        break;
      case 2:
        ShowContextDialog();
        break;


    }
    return true;
  }


  public void BackToLastActivity(View view) {
    Intent intent = new Intent(this, ImageViewActivity.class);
    startActivity(intent);
  }

  public void GoToNextActivity(View view) {
    Intent intent = new Intent(this, AdapterActivity.class);
    startActivity(intent);

  }
  private void LinkViewByTextView() {
    TextViewArrayAdapter adapter=new TextViewArrayAdapter(this,myData);
    ListView listView = (ListView) findViewById(R.id.AdapterView);
    listView.setAdapter(adapter);
  }

  private void LinkViewByCheckedTextView() {
    CheckedViewArrayAdapter adapter = new CheckedViewArrayAdapter(this, myData2);
    ListView listView=(ListView) findViewById(R.id.CheckTextView);
    listView.setAdapter(adapter);
  }

  private void LoadShopView() {
    Intent intent = new Intent(this, ShopItemActivity.class);
    startActivity(intent);
  }

  private void ShowContextDialog() {

  }

  private void SetContextWindow() {
    Button button = (Button) findViewById(R.id.context_button);
    registerForContextMenu(button);

  }
  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("上下文菜单");
    menu.add(0, 1, 0, "1901010227");
    menu.add(0, 2, 1, "王楚杰");
  }

}
